package com.fucai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fucai.controller.dto.CheckNumDTO;
import com.fucai.controller.dto.CheckOneNumOfOccurrencesDTO;
import com.fucai.controller.dto.SaveDataDTO;
import com.fucai.controller.dto.ZjLuStatisticsDTO;
import com.fucai.controller.vo.AddDatasVO;
import com.fucai.controller.vo.CheckNumVO;
import com.fucai.controller.vo.UpdateFucai3dReqVO;
import com.fucai.entity.Fucai3d;
import com.fucai.service.Fucai3dService;
import com.fucai.utils.ListConverterUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 福彩3D
 * @author: zhuchi
 * @create: 2024-03-22 13:20
 * @Version 1.0
 **/
@Api(tags = "福彩3D")
@Slf4j
@RestController
@RequestMapping("/fucai/3d")
public class Fucai3dController {

    @Autowired
    private Fucai3dService fucai3dService;

    /**
     * 添加最新开奖信息
     * @param saveDataDTO
     * @return
     */
    @ApiOperation(value = "添加最新开奖信息")
    @PostMapping("/addData")
    public Integer addData(@Validated @RequestBody SaveDataDTO saveDataDTO){
        int result = fucai3dService.addData(saveDataDTO);

        return result;
    }

    /**
     * 获取最新一期数据
     * @return
     */
    @ApiOperation(value = "获取最新一期数据")
    @GetMapping("/getNewData")
    public Fucai3d getNewData(){
        Fucai3d fucai3d = this.fucai3dService.getNewData();
        return fucai3d;
    }

    /**
     * 中奖号路数占比统计
     * @param id 从哪一期开始往前
     * @param qiQuantity 往前查询期数
     * @return
     */
    @ApiOperation(value = "中奖号路数占比统计")
    @GetMapping("/getZjLuStatistics")
    public List<ZjLuStatisticsDTO> getZjLuStatistics(@RequestParam(value = "id", required = false) Integer id, @RequestParam("qiQuantity") Integer qiQuantity){
        List<ZjLuStatisticsDTO> list = this.fucai3dService.getZjLuStatistics(id, qiQuantity);
        return list;
    }

    /**
     * 根据ID获取数据
     * @return
     */
    @ApiOperation(value = "根据ID获取数据")
    @GetMapping("/getDataById")
    public Fucai3d getDataById(@RequestParam Integer id){
        Fucai3d fucai3d = this.fucai3dService.getById(id);
        return fucai3d;
    }

    /**
     * 获取近多少期数据
     * @param quantity 数量
     * @return
     */
    @ApiOperation(value = "获取近多少期数据")
    @ApiImplicitParam(name = "quantity", value = "往前查询期数", required = true, example = "10")
    @GetMapping("/getFucai3dListByStartId")
    public List<Fucai3d> getFucai3dListByStartId(@RequestParam(value = "quantity") Integer quantity){
        List<Fucai3d> list = this.fucai3dService.getFucai3dListByStartId(quantity);
        return list;
    }


    /**
     * 自动计算相关信息并更新数据
     * @param reqVO
     * @return
     */
    @ApiOperation(value = "自动计算相关信息并更新数据")
    @PostMapping("/updateData")
    public Integer updateData(@RequestBody UpdateFucai3dReqVO reqVO){
        int result = 0;

        if (!CollectionUtils.isEmpty(reqVO.getIds())){
            for (Integer id : reqVO.getIds()) {
                Fucai3d fucai3d = this.fucai3dService.getById(id);
                if (null != fucai3d){
                    int updateState = fucai3dService.updateData(fucai3d);
                    if (updateState > 0){
                        result++;
                    }
                }
            }
        }else{
            LambdaQueryWrapper<Fucai3d> queryWrapper = new LambdaQueryWrapper<>();
            List<Fucai3d> list = this.fucai3dService.list(queryWrapper);
            for (Fucai3d fucai3d : list) {
                int updateState = fucai3dService.updateData(fucai3d);
                if (updateState > 0){
                    result++;
                }
            }
        }

        return result;
    }

    /**
     * 校验所选号码
     * @param checkNumDTO
     * @return
     */
    @ApiOperation(value = "校验所选号码")
    @PostMapping("/checkNum")
    public List<CheckNumVO> checkNum(@Validated @RequestBody CheckNumDTO checkNumDTO){
        List<Fucai3d> result = fucai3dService.checkNum(checkNumDTO);
        List<CheckNumVO> checkNumVOS = ListConverterUtil.converterList(result, CheckNumVO.class);
        return checkNumVOS;
    }

    /**
     * 统计某个数字出现的次数
     * @param dto
     * @return
     */
    @ApiOperation(value = "统计某个数字出现的次数")
    @PostMapping("/checkOneNumOfOccurrences")
    public List<CheckNumVO> checkOneNumOfOccurrences(@Validated @RequestBody CheckOneNumOfOccurrencesDTO dto){
        List<Fucai3d> result = fucai3dService.checkOneNumOfOccurrences(dto);
        List<CheckNumVO> checkNumVOS = ListConverterUtil.converterList(result, CheckNumVO.class);
        return checkNumVOS;
    }

    /**
     * 根据选定号码组号
     * @param numbers 选定的号码
     * @param baozi 是否算入豹子号true:算入,false:不算入
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "numbers", value = "选定的号码", required = true, example = "1,2,3"),
            @ApiImplicitParam(name = "baozi", value = "是否算入豹子号true:算入,false:不算入", required = true, example = "true")
    })
    @ApiOperation(value = "根据选定号码组号")
    @GetMapping("/groupNum")
    public List<String> groupNum(@RequestParam("numbers") String numbers, @RequestParam(value = "baozi", defaultValue = "true") Boolean baozi){
        List<Integer> numbersArr = Arrays.stream(numbers.split(",")).map(Integer::parseInt).collect(Collectors.toList());

        List<String> result = fucai3dService.groupNum(numbersArr, baozi);

        return result;
    }

    /**
     * 添加往期数据
     * @return
     */
    @ApiOperation(value = "添加往期数据")
    @PostMapping("/addDatas")
    public Integer addDatas(@RequestBody AddDatasVO vo){
        String kaiji = "2025135210238,2025134619530,2025133349618,2025132371407,2025131728328,2025130653817,2025129375563,2025128862279,2025127431964,2025126791853,2025125380923,2025124307605,2025123514847,2025122627154,2025121306476,2025120751712,2025119923513,2025118864760,2025117732365,2025116381431,2025115130892,2025114843971,2025113081374,2025112765085,2025111564917,2025110165589,2025109196287,2025108496431,2025107381895,2025106157473,2025105754968,2025104167817,2025103641492,2025102270204,2025101034657,2025100892599,2025099213738,2025098365651,2025097761429,2025096034513,2025095834942,2025094684701,2025093540486,2025092901934,2025091719553,2025090318924,2025089651730,2025088386598,2025087472164,2025086163288,2025085639759,2025084846927,2025083096703,2025082417418,2025081807607,2025080850983,2025079153685,2025078016251,2025077147463,2025076538514,2025075867840,2025074810651,2025073821819,2025072576537,2025071908483,2025070310872,2025069308450,2025068978923,2025067705561,2025066925973,2025065603862,2025064320502,2025063498549,2025062308468,2025061139835,2025060319913,2025059743746,2025058285491,2025057392268,2025056261192,2025055620413,2025054062712,2025053958408,2025052547519,2025051098674,2025050169892,2025049568215,2025048034374,2025047164289,2025046412379,2025045872816,2025044741629,2025043497940,2025042428237,2025041530394,2025040014853,2025039568405,2025038390761,2025037053208,2025036683934,2025035039407,2025034598537,2025033270895,2025032614562,2025031469438,2025030253261,2025029201987,2025028652735,2025027749413,2025026784375,2025025283249,2025024182106,2025023326279,2025022219935,2025021965408,2025020457173,2025019356342,2025018514209,2025017604852,2025016126374,2025015302146,2025014204926,2025013085325,2025012379467,2025011321569,2025010219738,2025009147152,2025008265702,2025007560354,2025006569238,2025005850892,2025004810925,2025003194568,2025002206421,2025001315068,2024352769735,2024351983671,2024350875975,2024349851146,2024348876591,2024347306638,2024346645143,2024345103408,2024344132568,2024343632542,2024342342738,2024341708594,2024340971139,2024339610273,2024338308716,2024337059325,2024336840037,2024335458454,2024334918612,2024333839257,2024332539768,2024331649539,2024330235962,2024329862418,2024328126169,2024327530835,2024326468214,2024325721469,2024324926750,2024323164877,2024322206415,2024321218976,2024320251438,2024319034062,2024318073375,2024317478812,2024316159430,2024315358914,2024314718133,2024313291948,2024312408371,2024311870493,2024310735763,2024309820873,2024308471617,2024307652844,2024306849349,2024305106653,2024304916674,2024303321220,2024302830519,2024301139607,2024300231023,2024299982807,2024298231913,2024297680058,2024296704473,2024295763950,2024294947305,2024293849822,2024292305830,2024291296542,2024290593583,2024289734611,2024288986784,2024287310372,2024286287063,2024285341337,2024284248502,2024283710626,2024282154196,2024281328672,2024280604918,2024279514393,2024278648974,2024277152108,2024276982764,2024275157241,2024274326672,2024273749463,2024272859717,2024271275850,2024270638313,2024269975174,2024268621808,2024267249782,2024266182981,2024265930114,2024264590909,2024263805594,2024262894754,2024261246361,2024260472832,2024259790057,2024258256606,2024257462189,2024256253223,2024255580265,2024254942380,2024253125724,2024252063380,2024251943537,2024250165721,2024249928574,2024248045930,2024247593429,2024246465951,2024245940797,2024244786555,2024243674633,2024242130413,2024241163477,2024240621758,2024239107002,2024238960679,2024237943115,2024236640591,2024235842673,2024234730112,2024233921594,2024232615898,2024231850347,2024230631241,2024229915870,2024228109027,2024227935295,2024226571243,2024225381619,2024224675099,2024223362923,2024222123339,2024221195872,2024220408246,2024219742539,2024218614033,2024217569845,2024216074274,2024215569770,2024214602841,2024213746456,2024212561187,2024211031008,2024210412570,2024209204492,2024208415565,2024207670762,2024206079404,2024205251470,2024204174863,2024203947902,2024202542798,2024201506378,2024200761164,2024199297441,2024198062484,2024197597497,2024196435828,2024195809651,2024194602385,2024193120177,2024192895557,2024191315271,2024190916916,2024189509699,2024188052677";
        vo.setKaijiDatas(kaiji);
        Integer result = this.fucai3dService.addDatas(vo);
        return result;
    }

}
